
<!DOCTYPE html>
<html lang="en" class="loading">
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Docker学习之Dockerfile书写 - kyralo</title>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate">
    <meta name="keywords" content="kyralo,"> 
    <meta name="description" content="Dockerfile的定义Dockerfile是一个包含用于组合映像的命令的文本文档, 
Docker通过读取Dockerfile中的指令自动生成映像。
构建镜像时使用docker build命令构,"> 
    <meta name="author" content="kyralo"> 
    
    <link rel="icon" href="https://cdn.jsdelivr.net/gh/kyralo/cdn/ico/favicon.ico"> 
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kyralo/cdn@v1.0.2/css/diaspora.css">
    <!-- <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> -->
    <script>
         (adsbygoogle = window.adsbygoogle || []).push({
              google_ad_client: "ca-pub-8691406134231910",
              enable_page_level_ads: true
         });
    </script>
    <!-- <script async custom-element="amp-auto-ads"
        src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js">
    </script> -->
</head>
</html>
<body class="loading">
    <span id="config-title" style="display:none">kyralo</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="icon-home image-icon" href="javascript:;" data-url="https://kyralo.github.io"></a>
    <!-- <div title="播放/暂停" class="icon-play"></div> -->
    <h3 class="subtitle">Docker学习之Dockerfile书写</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">Docker学习之Dockerfile书写</h1>
        <div class="stuff">
            <span>十二月 20, 2019</span>
            
  <ul class="post-tags-list"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/Docker/">Docker</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/Dockerfile/">Dockerfile</a></li></ul>


        </div>
        <div class="content markdown">
            <h3 id="Dockerfile的定义"><a href="#Dockerfile的定义" class="headerlink" title="Dockerfile的定义"></a>Dockerfile的定义</h3><pre><code>Dockerfile是一个包含用于组合映像的命令的文本文档, 
Docker通过读取Dockerfile中的指令自动生成映像。
构建镜像时使用docker build命令构建,
可以在docker build命令中使用-f标志指向文件系统中任何位置的Dockerfile。</code></pre><h3 id="Dockerfile文件说明"><a href="#Dockerfile文件说明" class="headerlink" title="Dockerfile文件说明"></a>Dockerfile文件说明</h3><pre><code>基础镜像信息、维护者信息、镜像操作指令和容器启动时执行指令
`#` 为Dockerfile的注释

为了指定基本映像，第一条指令必须是FROM。一个声明以＃字符开头
则被视为注释。可以在Docker文件中使用RUN，CMD，FROM，EXPOSE，
ENV等指令.</code></pre><h3 id="Dockerfile指令"><a href="#Dockerfile指令" class="headerlink" title="Dockerfile指令"></a>Dockerfile指令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">FROM: </span><br><span class="line">指定基础镜像,第一个命令必填.</span><br><span class="line"></span><br><span class="line">e.g </span><br><span class="line">FROM mysql:8.0</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">LABEL:</span><br><span class="line">添加标签,帮助组织镜像、记录许可信息、辅助自动化构建等</span><br><span class="line"></span><br><span class="line">e.g</span><br><span class="line">LABEL com.example.version=&quot;0.0.1-beta&quot;</span><br><span class="line"></span><br><span class="line">LABEL vendor=&quot;ACME Incorporated&quot;</span><br><span class="line"></span><br><span class="line">LABEL com.example.release-date=&quot;2019-12-20&quot;</span><br><span class="line"></span><br><span class="line">LABEL com.example.version.is-production=&quot;&quot;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">RUN:</span><br><span class="line">用于在镜像容器中执行命令</span><br><span class="line">有以下两种命令执行方式：</span><br><span class="line">shell执行:</span><br><span class="line">    RUN &lt;command&gt;</span><br><span class="line">    e.g RUN ls /</span><br><span class="line"></span><br><span class="line">exec执行:</span><br><span class="line">    RUN [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;]</span><br><span class="line"></span><br><span class="line">    e.g </span><br><span class="line">    RUN apk update</span><br><span class="line">    RUN [&quot;/etc/execfile&quot;, &quot;arg1&quot;, &quot;arg1&quot;]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">注意:</span><br><span class="line">    RUN指令创建的中间镜像会被缓存，并会在下次构建中使用。</span><br><span class="line">    如果不想使用这些缓存镜像，可以在构建时指定--no-cache参数，</span><br><span class="line">    如：docker build --no-cache</span><br><span class="line">    此外,不要使用 RUN apt-get(yum) upgrade 或 dist-upgrade，</span><br><span class="line">    因为许多基础镜像中的「必须」包不会在一个非特权容器中升级。</span><br><span class="line">    如果基础镜像中的某个包过时了，你应该联系它的维护者。</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">CMD:</span><br><span class="line">用于执行目标镜像中包含的软件,可以包含参数</span><br><span class="line">构建容器后调用，也就是在容器启动时才进行调用</span><br><span class="line">格式: CMD [&quot;executable&quot;, &quot;param1&quot;, &quot;param2&quot;...]</span><br><span class="line">e.g </span><br><span class="line">CMD [&quot;apache2&quot;, &quot;-DFOREGROUND&quot;]</span><br><span class="line"></span><br><span class="line">多数情况下，CMD 都需要一个交互式的 shell (bash, Python, perl 等)</span><br><span class="line">如 CMD [&quot;PHP&quot;, &quot;-a&quot;]</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">EXPOSE:</span><br><span class="line">指定于外界交互的端口</span><br><span class="line"></span><br><span class="line">格式: EXPOSE 8080</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">ENV:</span><br><span class="line">设置环境变量</span><br><span class="line">为容器中安装的程序更新 PATH 环境变量</span><br><span class="line"></span><br><span class="line">e.g</span><br><span class="line">ENV JAVA_HOME /usr/local/java/jdk1.8</span><br><span class="line">ENV PATH /usr/local/nginx/bin:$PATH</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">ADD 和 COPY :</span><br><span class="line">ADD 和 COPY 功能类似,一般优先使用 COPY,它比 ADD 更透明</span><br><span class="line">COPY 只支持简单将本地文件拷贝到容器中</span><br><span class="line">ADD 有一些并不明显的功能（比如本地 tar 提取和远程 URL 支持）</span><br><span class="line">ADD 的最佳用例是将本地 tar 文件自动提取到镜像中 (ADD rootfs.tar.xz)</span><br><span class="line"></span><br><span class="line">如果Dockerfile中有多个步骤需要使用上下文中不同的文件。</span><br><span class="line">单独COPY每个文件，而不是一次性的COPY所有文件,</span><br><span class="line">这将保证每个步骤的构建缓存只在特定的文件变化时失效</span><br><span class="line"></span><br><span class="line">为了让镜像尽量小，最好不要使用 ADD 指令从远程 URL 获取包，</span><br><span class="line">而是使用 curl 和 wget</span><br><span class="line">这样你可以在文件提取完之后删掉不再需要的文件来避免在镜像中额外添加一层</span><br><span class="line"></span><br><span class="line">最好使用如下写法:</span><br><span class="line">    RUN mkdir -p /usr/src/things \</span><br><span class="line">        &amp;&amp; curl -SL http://example.com/big.tar.xz \</span><br><span class="line">        | tar -xJC /usr/src/things \</span><br><span class="line">        &amp;&amp; make -C /usr/src/things all</span><br><span class="line"></span><br><span class="line">(上面使用的管道操作，所以没有中间文件需要删除,即不需要额外删除big.tar.xz。)</span><br><span class="line"></span><br><span class="line">而不是</span><br><span class="line">    ADD http://example.com/big.tar.xz /usr/src/things/</span><br><span class="line"></span><br><span class="line">对于其他不需要 ADD 的自动提取功能的文件或目录，你应该使用 COPY。</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">ENTRYPOINT:</span><br><span class="line">配置容器，使其可执行化</span><br><span class="line">ENTRYPOINT 的最佳用处是设置镜像的主命令，允许将镜像当成命令本身来运行</span><br><span class="line">e.g:</span><br><span class="line">    ENTRYPOINT [&quot;demo&quot;]</span><br><span class="line">    CMD [&quot;--help&quot;]</span><br><span class="line">    docker run demo</span><br><span class="line"></span><br><span class="line">ENTRYPOINT 指令也可以结合一个辅助脚本使用，和命令行风格类似</span><br><span class="line">e.g</span><br><span class="line">脚本被拷贝到容器，并在容器启动时通过 ENTRYPOINT 执行：</span><br><span class="line">COPY ./demo.sh /</span><br><span class="line">ENTRYPOINT [&quot;/demo.sh&quot;]</span><br><span class="line">该脚本可以让用户用几种不同的方式和应用交互</span><br><span class="line">1. 简单地启动 (docker run app)</span><br><span class="line">2. 传递参数 (docker run app app --help)</span><br><span class="line">3. 启动其他工具 (docker run --rm -it app bash)</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">VOLUME 指令用于暴露任何数据库存储文件，</span><br><span class="line">配置文件，或容器创建的文件和目录</span><br><span class="line"></span><br><span class="line">格式：</span><br><span class="line">    VOLUME [&quot;/path/to/dir&quot;]</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">USER:</span><br><span class="line">指定运行容器时的用户名或 UID，后续的 RUN 也会使用指定用户</span><br><span class="line">使用USER指定用户时，可以使用用户名、UID或GID，或是两者的组合</span><br><span class="line">当服务不需要管理员权限时，可以通过该命令指定运行用户,</span><br><span class="line">并且可以在之前创建所需要的用户</span><br><span class="line"></span><br><span class="line">格式:</span><br><span class="line">　　USER user</span><br><span class="line">　　USER user:group</span><br><span class="line">　　USER uid</span><br><span class="line">　　USER uid:gid</span><br><span class="line">　　USER user:gid</span><br><span class="line">　　USER uid:group</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">WORKDIR:</span><br><span class="line">工作目录，类似于cd命令</span><br><span class="line"></span><br><span class="line">格式：</span><br><span class="line">    WORKDIR /path/to/workdir</span><br><span class="line"></span><br><span class="line">通过WORKDIR设置工作目录后，</span><br><span class="line">Dockerfile中其后的命令RUN、CMD、ENTRYPOINT、ADD、COPY等命令</span><br><span class="line">都会在该目录下执行在使用docker run运行容器时 可以通过-w参数覆盖构建时所设置的工作目录。</span><br></pre></td></tr></table></figure>


        </div>
        
    <div id='gitalk-container' class="comment link"
        data-ae='false'
        data-ci='eeec43124e53633945c0'
        data-cs='e2332cffb30ae24f185c1701286958cddd748ffb'
        data-r='kyralo.github.io'
        data-o='kyralo'
        data-a='kyralo'
        data-d='false'
    >查看评论</div>


    </div>
    
        <div class='side'>
            <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Dockerfile的定义"><span class="toc-number">1.</span> <span class="toc-text">Dockerfile的定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Dockerfile文件说明"><span class="toc-number">2.</span> <span class="toc-text">Dockerfile文件说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Dockerfile指令"><span class="toc-number">3.</span> <span class="toc-text">Dockerfile指令</span></a></li></ol>
        </div>
    
</div>


    </div>
</div>
</body>
<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/plugin.js"></script>
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/diaspora.js"></script>
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/kyralo/cdn/css/photoswipe.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/kyralo/cdn/css/default-skin.css">
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/photoswipe.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/kyralo/cdn/js/photoswipe-ui-default.min.js"></script>

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<!-- Google Analytics -->
<script type="text/javascript">
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-146789315-1', 'auto');
  ga('send', 'pageview');
</script>
<!-- End Google Analytics -->


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10/dist/APlayer.min.css">
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10/dist/APlayer.min.js"></script>
<style>
  .aplayer .aplayer-lrc {
    height: 35px;
  }
  .aplayer .aplayer-lrc p{
    font-size: 16px;
    font-weight: 700;
    line-height: 18px !important;
  }
  .aplayer .aplayer-lrc p.aplayer-lrc-current{
    color: #FF1493;
  }
  .aplayer.aplayer-narrow .aplayer-body{
    left: -66px !important;
  }
  .aplayer.aplayer-fixed .aplayer-lrc {
    display: none;
  }
  .aplayer .aplayer-lrc.aplayer-lrc-hide {
      display:none !important;
  }
  .aplayer.aplayer-fixed .lrc-show {
    display: block;
    background: rgba(255, 255, 255, 0.8);
  }
</style>
<div class="aplayer"

    data-id="2725346449"

    data-server="netease"

    data-type="playlist"

    data-fixed="true"

    data-autoplay="false"

    data-loop="all"

    data-order="random"

    data-preload="auto"

    data-volume="0.7"

    data-mutex="true"

</div>
<script src="https://cdn.jsdelivr.net/npm/meting@1.2/dist/Meting.min.js"></script>
<script>
  $(function(){
    $('body').on('click', '.aplayer', function(){
      if($('.aplayer-button').hasClass('aplayer-play')) {
        $('.aplayer-lrc').removeClass('lrc-show');
      } else {
        $('.aplayer-lrc').addClass('lrc-show');
      }
    })
  });
</script>
</html>
